package com.xauto.pioneer.appuser.model;

import com.xauto.pioneer.appuser.util.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;

/**
 * 返回数据状态实体
 * Created by Lemon on 15-03-04.
 */
public class JSONStatus extends BaseModel implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** 错误标识码*/
	public int error_code;
	/** 错误提示详情*/
	public String error_description;
	/** 请求是否成功*/
	public Boolean isSuccess = false;
	/** 数据JSON*/
	public JSONObject data;
	/** 数据JSONArray*/
	public JSONArray dataArray;
	/** */
	public PageInfo pageInfo;

	@Override
	public JSONStatus parse(JSONObject jsonObject) {
		String succeed = jsonObject.optJSONObject("status").optString("succeed");//成功标石 1-成功，0-失败
		if(!StringUtils.isBlank(jsonObject.optJSONObject("status").optString("error_code"))) {
			error_code = Integer.parseInt(jsonObject.optJSONObject("status").optString("error_code"));
		} else {
			error_code = 0;
		}
		error_description = jsonObject.optJSONObject("status").optString("error_desc");
		if(Integer.parseInt(succeed) == 1){
			isSuccess = true;
		}
		if(Integer.parseInt(succeed) == 0){
			isSuccess = false;
		}
		data = jsonObject.optJSONObject("data");
		dataArray = jsonObject.optJSONArray("data");
		pageInfo = new PageInfo().parse(jsonObject.optJSONObject("paginated"));
		return this;
	}

	@Override
	public String toString() {
		return "{" +
				"error_code=" + error_code +
				", error_description='" + error_description + '\'' +
				", isSuccess=" + isSuccess +
				", data=" + data +
				", dataArray=" + dataArray +
				", pageInfo=" + pageInfo +
				'}';
	}
}
